package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Test1 {
    public String convert1(String s, int numRows) {
        if (numRows == 1) return s;
        int interval = (numRows-1)*2-1;
        interval = Math.max(interval,0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = i; j < s.length(); j+=interval+1) {
                sb.append(s.charAt(j));
            }
        }
        return sb.toString();
    }

    //leetcode Z字形变换 https://leetcode.cn/problems/zigzag-conversion/description/?envType=study-plan-v2&envId=top-interview-150
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        List<List<Character>> list = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            list.add(new ArrayList<>());
        }
        int index = 0;
        boolean flg = false;
        for (int i = 0; i < s.length(); i++) {
            list.get(index).add(s.charAt(i));
            if (!flg) index++;
            else index--;
            if (index == 0) flg = false;
            else if (index == numRows-1) flg = true;
        }
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (char c : list.get(i)) {
                ret.append(c);
            }
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        String ret = test1.convert("PAYPALISHIRING",3);
        System.out.println(ret);
    }
}
